import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
import {message, Button,Modal } from "antd";
import * as Apis from '../../apis/info';
import { ListStore } from '../listStore';
import { specialDialogStore } from '../dialog/specialDialogStore';
import { rewardDialogStore } from '../dialog/rewardDialogStore';
import {Text} from '../../components/util/text';
const text = new Text();

const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export default class adjustListStore extends ListStore {

    @observable responseData = {};
    @observable title = "";
    @observable dialogTitle = "";   //弹出框title
    @observable status = {
        loading: false,
        visible: false,
    }
    @observable form = new WeaForm();
    @observable condition = [];
    @observable isRight = false;        //判断是否有权限
    @observable loadingSpin = true;     //全局loading

    selectTabKey = "";    
    @observable tabs = [];          //regTop展示页签
    type = "";
    id = "";
    constructor(props) {
        super(props);
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    @action
    getInfoAdjustmentList = (params = {}) => {
        this.status.loading = true;
        //this.loadingSpin = true;
        this.searchForm = new WeaForm();
        this.searchCondition = [];
        let newParams = { 'pathId': this.selectTabKey };
        Apis.getInfoAdjustmentList(newParams).then(data => {
            if (data.api_status == '1') {
                this.loadingSpin = false;
                this.isRight = data.isRight;
                if (!this.isRight) {
                    return;
                }
                if (this.tabs.length == 0) {
                    // if (data.pathList.length > 1) {
                    //     this.tabs.push({ 'key': 'all', 'title': text.ALLPATH });
                    // } else {
                    //     this.selectTabKey = data.pathList[0].id;
                    // }
                    data.pathList.map(m => {
                        this.tabs.push({ 'key': m.id, 'title': m.name });
                    });
                    this.selectTabKey = data.pathList[0].id;
                }
                this.title = data.title;
                this.tableStore.getDatas(data.sessionkey, 1);
                this.responseData = { rightMenus: data.rightMenus };
                this.searchCondition = data.infoConditionGroup;
                this.searchForm && !this.searchForm.isFormInit && this.searchForm.initFormFields(data.infoConditionGroup);
                this.status.loading = false;
            } else {
                message.error(data.api_errormsg);
            }
        })
    }

    @action
    doSearch = (params = {}) => {
        this.status.loading = true;
        //获取表单的参数值
        const searchParamsAd = this.searchForm.getFormParams();
        const newParams = {'pathId': this.selectTabKey,...searchParamsAd };
        Apis.getInfoAdjustmentList(newParams).then(data => {
            if (data.api_status == '1') {
                this.tableStore = new TableStore();
                this.tableStore.getDatas(data.sessionkey, 1);
                this.status.loading = false;
            } else {
                message.error(data.api_errormsg);
            }
            
        })
    }

    @action
    getInfoAdjustmentCondition = (params = {}) => {
        this.type = params.type;
        this.id = params.id;
        this.form = new WeaForm();
        this.condition = [];
        let newParams = {id:this.id, 'pathId': this.selectTabKey };
        Apis.getInfoAdjustmentCondition(newParams).then(data => {
            if (data.api_status == '1') {
                if (!data.isRight) {
                    message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                    return;
                }
                this.setState({ visible: true });
                this.condition = data.infoConditionGroup;
                this.form && !this.form.isFormInit && this.form.initFormFields(this.condition);
            } else {
                message.error(data.api_errormsg);
            }
            
        })
    }

    //删除奖惩信息
    @action
    deleteInfoAdjustment = (params = {}) => {
        let _this = this;
        Modal.confirm({
            title: text.SYSTEMTIP,
            content: text.SUREDELETE,
            onOk() {
                Apis.deleteInfoAdjustment(params).then(data => {
                    if (data.api_status == '1') {
                        if (!data.isRight) {
                            message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                            return;
                        }
                        message.success(text.DELSUCCESS);
                        window._table.reLoad();
                    } else {
                        message.error(data.api_errormsg);
                    }
                });
            }
        });
    }

    saveInfoAdjustment = (params = {}) => {
        let formParams = this.form.getFormParams();
        let newParams = {...formParams, 'pathId': this.selectTabKey,type:this.type,id:this.id };
        this.form.validateForm().then(f => {
            if (f.isValid) {
                Apis.saveInfoAdjustment(newParams).then(data => {
                    if (data.api_status == '1') {
                        if (!data.isRight) {
                            message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                            return;
                        }
                        message.success(text.SAVESUCCESS);
                        window._table.reLoad();
                        this.setState({ visible: false });
                    } else {
                        message.error(data.api_errormsg);
                    }
                })
            } else {
                f.showErrors();
            }
        });
    }

    getRightMenu = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        rightMenus && rightMenus.length > 0 && rightMenus.map(m => {
            btnArr.push({
                key: m.type,
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                onClick: key => {
                    this.buttonClick(key);
                },
                disabled: this.tableStore.selectedRowKeys.length === 0 && m.type == 'BTN_DELETE'
            })
        });
        return btnArr;
    }

    //设置选中按钮
    @action
    changeTab = (key) => {
        this.selectTabKey = key;
        this.getInfoAdjustmentList();
    }


    /**
   * 上方按钮
   */
    getTopButton = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        rightMenus && rightMenus.length > 0 && rightMenus.map((m,index) => {
            btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@z89km6@${index}`} style={{ marginLeft: "10px" }} type={"primary"}
                    disabled={ m.type == 'BTN_DELETE' && this.tableStore.selectedRowKeys.length === 0}
                    onClick={() => {
                        this.buttonClick(m.type);
                    } }>
                    {m.menuName}
                </Button>
            );

        });
        return btnArr;
    }

    buttonClick(key) {
        if (key == "BTN_CREATE") {
            this.getInfoAdjustmentCondition({type:'add'});
            //this.setState({ visible: true });
        }
        if (key == "BTN_DELETE") {
            let selectedRowKeys = this.tableStore.selectedRowKeys;
            this.deleteInfoAdjustment({ids:selectedRowKeys.join(",")});
        }
    }

}